package se.webbzon.boltzmann.game.object.player.rootsie;

import se.webbzon.boltzmann.effect.CharacterSpinningEffect;
import se.webbzon.boltzmann.effect.EarthquakeEffect;
import se.webbzon.boltzmann.effect.GameCameraShakeEffect;
import se.webbzon.boltzmann.game.object.camera.GameCamera;
import se.webbzon.boltzmann.timer.Timer;
import se.webbzon.boltzmann.timer.TimerListener;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;
import se.webbzon.oschi01.worldobject.appearance.NoAppearance3D;

public class Earthquake extends WorldStaticObject {
	
	// The game camera associated with this fire bomb
	private final GameCamera camera;
		
	// The Rootsie character associated with this fire bomb
	private final RootsiePlayerCharacter rootsie;
	
	/*============================================================
	Constructors
	============================================================*/

	public Earthquake(RootsiePlayerCharacter rootsie, GameCamera camera) {
		super(WorldObject.onCreate, new NoAppearance3D());
		setPosition(rootsie.getX(),rootsie.getY(),rootsie.getZ());
		this.rootsie = rootsie;
		this.camera = camera;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Releases the earthquake upon the given world area. **/
	public void release(WorldArea world) {
		final double maxMagnitude = 64 * rootsie.getFocus();
		
		new GameCameraShakeEffect(camera,45,maxMagnitude).launch(world);
		
		for (int i = 0; i < 4; i++)
			new EarthquakeEffect(	this,
									maxMagnitude * (1 + Math.random()),
									15 + (int) (30 * Math.random())).launch(world);
		
		new CharacterSpinningEffect(rootsie,15).launch(world);
		rootsie.jump();
		
		Timer timer = new Timer(30);
		timer.addListener(new TimerListenerImpl());
		timer.start(world);
	}
	
	@Override public void onCreate(WorldArea world) {
		camera.setHover(160, 384);
		camera.setTarget(this);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	/** An implementation of a timer listener. **/
	private final class TimerListenerImpl implements TimerListener {

		@Override public void onAlarm(Timer timer) {
			camera.setTarget(rootsie);
			camera.resetHover();
		}
		
	}

}
